package com.sky.service.impl;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.interceptor.JwtTokenUserInterceptor;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImp implements ShoppingCartService {
    @Autowired
    ShoppingMapper shoppingMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    @Override
    public void addCart(ShoppingCartDTO shoppingCartDTO) {
        //先判断该商品是否在购物车
        //如果在，则数量加1
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);

        shoppingCart.setUserId(JwtTokenUserInterceptor.threadLocal.get());
        ShoppingCart list = shoppingMapper.selectCart(shoppingCart);
        if (list!=null)
        {
            //数量加一

            list.setNumber(list.getNumber()+1);
            //价格加一

            shoppingMapper.updateNumber(list);
        }
        else {
            Long dishid = shoppingCart.getDishId();
            if (dishid!=null)
            {
                Dish dish  = dishMapper.selectById(dishid);
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setName(dish.getName());
            }else {
                Long setmealid = shoppingCartDTO.getSetmealId();
                Setmeal setmeal = setmealMapper.selectById(setmealid);
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setName(setmeal.getName());
            }

            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingMapper.insert(shoppingCart);
        }





    }

    @Override
    public List<ShoppingCart> select() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(JwtTokenUserInterceptor.threadLocal.get());
      List<ShoppingCart> list = shoppingMapper.select(shoppingCart);
        return list;
    }

    @Override
    public void delete() {

        shoppingMapper.delete();
    }

    @Override
    public void deleteById(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(JwtTokenUserInterceptor.threadLocal.get());
        //查询对应的数量
        ShoppingCart shoppingCart1 = shoppingMapper.selectCart(shoppingCart);
        //通过用户和菜品或者套餐id减去相应的数量

        Integer  number = shoppingCart1.getNumber();
        if (number!=1){
        shoppingCart1.setNumber(shoppingCart1.getNumber()-1);
        shoppingMapper.updateNumber(shoppingCart1);}
        else {
            shoppingMapper.deleteById(shoppingCart);
        }
    }
}
